package upload;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

class SendFile extends Thread
{
	private Socket socket;
	private String path;
	private String ip ;
	private int port;
	
	public SendFile(String path,String ip,int port){
		this.path = path;
		this.port=port;
		this.ip=ip;
	}

	public void run(){
		
		InputStream is = null;
		OutputStream os = null;
		File file = null;

		try
		{
			file = new File(path);
			if (file.exists())
			{
				is = new FileInputStream(file);
				
				socket = new Socket(ip,port);
				os = socket.getOutputStream();

				String fileName = file.getName();
				DataOutputStream dos = new DataOutputStream(os);

				dos.writeUTF(fileName);

				int len = 0;
				int bufferSize = 100000;
				byte[] b = new byte[bufferSize];
				while (true)
				{
					len = is.read(b);
					if (len==-1)
					{
						break;
					}

					
				os.write(b,0,len);
				os.flush();
				}

			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally{
			
			try
			{
				if (is!=null)
				{
					is.close();
				}
				if (os!=null)
				{
					os.close();
				}
				socket.close();
			}
			catch (Exception ex)
			{
				ex.printStackTrace();
			}
		}
	}
}


